import {
  getFundingTable,
  editFundingRecord
} from '../../api/financial'
import {
  FUNDING_LIST,
  FUNDING_CURRENT_ROW,
  FUNDING_DATA_COUNT,
  FUNDING_HISTORY_DATA,
  FUNDING_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    fundingList: [],
    fundingDataCount: null,
    fundingPageSize: 20,
    fundingHistoryData: [],
    row: {
      tid: '',
      uname: '',
      commission: '',
      service: '',
      company: '',
      tel: '',
      type: '',
      state: '',
      price: '',
      ctime: '',
      des: '',
      paytype: '',
      tprice: '',
      alluse: ''
    }
  },
  getters: {
    fundingList: state => state.fundingList
  },
  mutations: {
    [FUNDING_LIST] (state, data) {
      state.fundingList = data
    },
    [FUNDING_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [FUNDING_DATA_COUNT] (state, data) {
      state.fundingDataCount = data
    },
    [FUNDING_HISTORY_DATA] (state, data) {
      state.fundingHistoryData = data
    },
    [FUNDING_PAGE_SIZE] (state, data) {
      state.fundingPageSize = data
    }
  },
  actions: {
    async getFundingList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getFundingTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          tid: item.tid,
          uname: item.uname,
          commission: item.commission,
          service: item.service,
          company: item.company,
          tel: item.tel,
          type: item.type,
          state: item.state,
          price: item.price,
          ctime: item.ctime,
          des: item.des,
          paytype: item.paytype,
          tprice: item.tprice,
          alluse: item.alluse
        }]
      })
      commit(FUNDING_LIST, newData)
      commit(FUNDING_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.funding.fundingPageSize) {
        commit(FUNDING_HISTORY_DATA, newData)
      } else {
        commit(FUNDING_HISTORY_DATA, newData.slice(0, rootState.funding.fundingPageSize))
      }
      return resp
    },
    async editFundingData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editFundingRecord(data)
      return resp
    },
    async getFundingCurrentRow ({ commit, rootState }, row) {
      commit(FUNDING_CURRENT_ROW, row)
    },
    async changeFundingPage ({ commit, rootState }, index) {
      let data = rootState.funding.fundingList
      let _start = (index - 1) * rootState.funding.fundingPageSize
      let _end = index * rootState.funding.fundingPageSize
      commit(FUNDING_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeFundingPageSize ({ commit, rootState }, value) {
      commit(FUNDING_PAGE_SIZE, value)
    }
  }
}
